﻿#ifndef MYSQLDB_H
#define MYSQLDB_H

#include "glog/logging.h"
#include <mysql.h>
#include "QStringList"
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <iostream>
#include <utility>

class MySQLDB
{
public:
    MySQLDB();
    ~MySQLDB();

    void initialize(char *host, char *user,  char *pwd, char *dbname, int port);
    bool ConnectDatabase();    		//函数声明
    void FreeConnect();

    bool InsertData(char* sql);		//增
    bool ModifyData(char* sql);		//改
    bool DeleteData(char *sql);		//删
    bool SelectData(const char *sql, std::unordered_set<std::string> &result, unsigned int &rows);
    bool SelectData(const char *sql, std::unordered_map<std::string, std::string> &result, unsigned int &colCount, unsigned int &rowCount); // select

    bool IsConnect();				//数据库是否连接

private:
    bool m_isConnect = false;

    MYSQL           mysql;			//mysql连接
    MYSQL_FIELD*    fd;             //字段列数组
    MYSQL_RES*      res;			//这个结构代表返回行的一个查询结果集
    MYSQL_ROW       row;
    char            field[32][32];  //存字段名二维数组

    std::string     m_host;
    std::string     m_user;
    std::string     m_pwd;
    std::string     m_db;
    int             m_port;

};

#endif // MYSQLDB_H
